package wetogether.community.control;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import wetogether.community.service.BoardService;
import wetogether.community.vo.AttachmentVO;
import wetogether.community.vo.BoardVO;


public class QnaBoardController extends MultiActionController {
	private Log log = LogFactory.getLog(getClass());
	private BoardService boardService;	
	
	public void setBoardService(BoardService boardService) {
		this.boardService = boardService;
	}
	
	public ModelAndView addContent(HttpServletRequest request,HttpServletResponse response) {
		System.out.println("in addContent");		
		ModelAndView mv=new ModelAndView();		
		BoardVO bvo=new BoardVO();
		AttachmentVO avo = new AttachmentVO();	
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		
		try {			
			bind(request, bvo);
			MultipartFile multipartFile= multipartRequest.getFile("uploadFile");			
			if(!multipartFile.isEmpty()) {
				String fileName=multipartFile.getOriginalFilename();
				String filePath=System.currentTimeMillis()+"_"+multipartFile.getOriginalFilename();
				String path=getServletContext().getRealPath("/")+"upload";
				File dir=new File(path);	
				if(!dir.isDirectory()) {
					dir.mkdirs();
					System.out.println(path+" create...");
				}
				byte b[]=multipartFile.getBytes();
				//new File(directory, fileName)
				File createFile=new File(path,filePath);
				FileOutputStream fos=new FileOutputStream(createFile);
				fos.write(b);
				fos.flush();
				fos.close();
				System.out.println(createFile.getName()+" upload done!!");	
				
				avo.setFileName(fileName);
				avo.setFilePath(filePath);
				avo.setFileType(multipartFile.getContentType());
				avo.setFileSize(multipartFile.getSize());
				bvo.setAttachmentVO(avo);				
			}
			boardService.addContent(bvo);
			mv.addObject("boardVO", bvo);
			mv.addObject("nowPage", request.getParameter("nowPage"));
		} catch (Exception e) {
			mv.addObject("info", e.getMessage());
			e.printStackTrace();
		}
		mv.setViewName("community/show_content");
		return mv; 
	}
	public ModelAndView getContentList(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv=new ModelAndView();
		int locationId=Integer.parseInt(request.getParameter("locationId"));
		int boardId=Integer.parseInt(request.getParameter("boardId"));
		int page=Integer.parseInt(request.getParameter("page"));		
		try {
			mv.addObject("lvo", boardService.getContentList(locationId, boardId, page));
		} catch (SQLException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		mv.setViewName("community/list");
		return mv;
	}
	//글보기
	public ModelAndView getContent(HttpServletRequest request,HttpServletResponse response) {
		System.out.println("[Control]in getContent");
		ModelAndView mv=new ModelAndView();
		BoardVO bvo=new BoardVO();
		String nowPage=request.getParameter("nowPage");
		try {
			bind(request, bvo);
			System.out.println("[bvo] "+bvo);			
			mv.addObject("boardVO", boardService.getContent(bvo.getContentId()));
			mv.addObject("nowPage",nowPage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		mv.setViewName("community/show_content");
		return mv;
	}
	//답글
	public ModelAndView addReply(HttpServletRequest request,HttpServletResponse response) {
		System.out.println("in addReply");
		ModelAndView mv=new ModelAndView();
		BoardVO bvo=new BoardVO();
		String nowPage=request.getParameter("nowPage");	
		System.out.println(nowPage);
		try {
			bind(request, bvo);
			System.out.println("bvo: "+bvo);
			boardService.addReply(bvo);
			mv.addObject("boardVO", bvo);
			mv.addObject("nowPage",nowPage);
		} catch (Exception e) {
			mv.addObject("info", e.getMessage());
			e.printStackTrace();
		}
		mv.setViewName("community/show_content");
		return mv; 
	}
	public ModelAndView deleteContent(HttpServletRequest request,HttpServletResponse response) {
		System.out.println("hihi");
		ModelAndView mav=new ModelAndView();
		BoardVO bvo=new BoardVO();
		try {
			bind(request, bvo);
			boardService.deleteContent(bvo.getContentId());
			mav.addObject("lvo", boardService.getContentList(bvo.getLocationId(), bvo.getBoardId(), 1));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mav.setViewName("community/list");
		return mav;
	}
	
	public ModelAndView downloadAttachment(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mav=new ModelAndView();
		
		return mav;
	}
}
